package com.testmall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.testmall.entity.Category;
import com.testmall.entity.Product;
import com.testmall.mapper.ProductMapper;
import com.testmall.service.CategoryService;
import com.testmall.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: k23
 * @Date: 2022-06-21 16:44
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/product")
@Api(value = "商品接口", tags = "商品接口")
public class ProductController {
    @Autowired
    ProductService productService;

    @ApiOperationSupport(order = 1)
    @PostMapping("/save")
    @ApiOperation(value = "保存商品", notes = "保存商品")
    public Boolean saveProduct(Product product){
     return productService.save(product);
    }


  @DeleteMapping("delete/{id}")
  @ApiOperation(value = "删除商品", notes = "删除商品")
  public Boolean deleteProduct(@PathVariable("id") String id) {
        return productService.removeById(id);
  }

  @Autowired
  ProductMapper productMapper;
  @GetMapping("/query")
  @ApiOperation(value = "查询所有商品", notes = "查询所有商品")
  public List<Product> queryProduct() {
      List<Product> productList = productMapper.selectList(null);

      Page<Product> mqPage = new Page<>(1, 1); //显示第一页 一页一条
      Page<Product> p = productMapper.selectPage(mqPage, null);
      List<Product> list = p.getRecords();
      //System.out.printf("第%d页，共%d页，每页%d条，共%d条%n",p.getCurrent(),p.getPages(),p.getSize(),p.getTotal());
      //System.out.println(list);

      return productList;
  }


    @PostMapping("/get/{id}")
    @ApiOperation(value = "查询指定商品", notes = "查询指定商品")
  public Product getProduct(@PathVariable Long id) {
    return productService.getById(id);
  }

  @PostMapping("/update")
  @ApiOperation(value = "更新商品", notes = "更新商品")
  public Boolean update(Product product){
      return productService.updateById(product);
  }

  @Autowired
  CategoryService categoryService;
  @GetMapping("/query/category")
  @ApiOperation(value = "查询商品分类列表", notes = "查询商品分类列表")
  public List<Category> querycategory(@RequestParam(required = false) String id){
        return categoryService.queryCategory(id);
  }




    @PostMapping("/save/category")
    @ApiOperation(value = "增加商品分类", notes = "增加商品分类")
    public Boolean savecategory(Category category){
        return categoryService.save(category);
    }






}
